import 'dart:async';

import 'package:app_tracking_transparency/app_tracking_transparency.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

import '../../../dd_core/dd_config.dart';
import '../../../dd_core/dd_other.dart';
import '../../../r.dart';
import '../pages/dd_login_page.dart';

class DDGuideTabController extends GetxController {
  var pageIndex = 0.obs;
  final pageController = PageController();

  Timer? guideTimer;

  Timer? _timer;

  @override
  void onInit() {
    super.onInit();
    startTimer();
    requestTrackingAuth();
  }

  void startTimer() {
    guideTimer?.cancel();
    guideTimer = Timer.periodic(Duration(seconds: 2), (Timer timer) {
      next();
    });
  }

  Future<void> requestTrackingAuth() async {
    _timer = Timer.periodic(Duration(seconds: 1), (Timer timer) async {
      TrackingStatus status = await AppTrackingTransparency.trackingAuthorizationStatus;
      if (status == TrackingStatus.notDetermined) {
        await Future.delayed(const Duration(milliseconds: 200));
        await AppTrackingTransparency.requestTrackingAuthorization();
      } else {
        timer.cancel();
      }
    });
  }

  @override
  void onClose() {
    guideTimer?.cancel();
    _timer?.cancel();
    pageController.dispose();
    pageIndex.close();
    super.onClose();
  }

  void pageIndexChange(int index) {
    pageIndex.value = index;
    if (pageIndex.value < guideList.length - 1) {
      startTimer();
    } else {
      startTimer();
    }
  }

  void next() {
    final page = pageController.page!.toInt();
    if (page < guideList.length - 1) {
      animateToPage(page + 1);
    } else {
      navigator?.popUntil((route) => route.settings.name == DDRoutesConfig.splash);
      navigator?.push(DDCustomRoute(DDLoginPage()));
    }
  }

  void animateToPage(int page) {
    pageController.animateToPage(page,
        duration: Duration(milliseconds: 300), curve: Curves.ease);
  }

  List<Widget> get guideList => [
        GestureDetector(
          onTap: () {
            next();
          },
          child: SizedBox(
            child: Image.asset(
              R.assetsImagesGuide1,
              fit: BoxFit.fitWidth,
              width: double.infinity,
              gaplessPlayback: true,
            ),
            height: double.infinity,
          ),
        ),
        GestureDetector(
          onTap: () {
            next();
          },
          child: SizedBox(
            child: Image.asset(
              R.assetsImagesGuide3,
              fit: BoxFit.fitWidth,
              width: double.infinity,
              gaplessPlayback: true,
            ),
            height: double.infinity,
          ),
        ),
        GestureDetector(
          onTap: () {
            next();
          },
          child: SizedBox(
            child: Image.asset(
              R.assetsImagesGuide2,
              fit: BoxFit.fitWidth,
              width: double.infinity,
              gaplessPlayback: true,
            ),
            height: double.infinity,
          ),
        )
      ];
  bool get isLast => pageIndex.value == guideList.length - 1;
}
